package game;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.geom.Point2D;
import java.util.LinkedList;

public class Radar 
{
	class RadarObject
	{
		Color color;
		Rectangle posicao;
		
		public RadarObject(Color c, Rectangle r)
		{
			this.color = c;
			this.posicao = r;
		}
	}
	
	
	Rectangle rect = new Rectangle(660, 20, 120, 90);
	Rectangle mapa = new Rectangle(0,0,0,0);
	Rectangle fixedRectangle = rect;
	
	LinkedList<RadarObject> objects = new LinkedList<RadarObject>();
	float proporcao = 0.1f; // que e a mesma coisa que dividir por 12.5
	
	public void Update(Player p, Rectangle[] rectMapa, LinkedList<BaseEnemy> enemie, BaseMapa m, Rectangle baseCapacitor)
	{
		this.UpdatePosition(p);
		
		
		mapa = new Rectangle((int)(
				(int)m.getPosition().getX()*proporcao),
				(int)(m.getPosition().getY()*proporcao),
				(int)(m.sizeMap.x*proporcao),
				(int)(m.sizeMap.y*proporcao));
		
		
		objects.clear();
		
		objects.add(new RadarObject(Color.WHITE, this.mapa));
		
		objects.add(new RadarObject(Color.DARK_GRAY, this.Redimencionar(baseCapacitor)));
		
		objects.add(new RadarObject(Color.BLUE, 
					this.Redimencionar(p.getRectangle())
					));
		
		for(Rectangle r : rectMapa)
		{
			objects.add(new RadarObject(Color.GRAY,
						this.Redimencionar(r)
						));
		}
		
		for(BaseEnemy e : enemie)
		{
			objects.add(new RadarObject(Color.RED,
						this.Redimencionar(e.getRectangle())
								));		
		}
	}
	
	public void UpdatePosition(Player player)
	{
		
		if(player.ColisaoRect(new Rectangle(Game.screenWidth - 200, 0, 200, 200)))
		{
			if(rect.getX() < Game.screenWidth + 13)
				rect = new Rectangle( (int)(rect.getX() + 3.5f), (int)rect.getY(), rect.width, rect.height);
		}
		else if(rect.getX() > fixedRectangle.getX())
		{
			rect = new Rectangle((int)(rect.getX() - 1), (int)rect.getY(), rect.width, rect.height);
		}
		
	}
	
	public void Draw(Graphics2D g2d, Player p)
	{
		
		
		g2d.drawImage(LoadImages.getImage(47), rect.x - 13, rect.y - 13, null);
		
		Point2D p2 = new Point2D.Double(
				this.rect.getMaxX() - (this.mapa.getMaxX() + this.rect.getX()),
				this.rect.getMaxY() - (this.mapa.getMaxY() + this.rect.getY())
						);
		
		if(p2.getX() < 0)
			p2.setLocation(0, p2.getY());
		if(p2.getY() < 0)
			p2.setLocation(p2.getX(), 0);
		
		g2d.setColor(Color.WHITE);
		g2d.fillRect(this.rect.x, this.rect.y, this.rect.width, this.rect.height);
	
		
		for(RadarObject r : objects)
		{
			g2d.setColor(r.color);
			
			Rectangle x = new Rectangle(
					(int)(this.rect.getX() + r.posicao.getX() + p2.getX()),
					(int)(this.rect.getY() + r.posicao.getY() + p2.getY()),
					(int)r.posicao.getSize().getWidth(),
					(int)r.posicao.getSize().getHeight());
			
			if(this.rect.intersects(x))
			{
				if(x.y <= this.rect.y)
					x = new Rectangle(x.x, this.rect.y, x.width, x.height - (this.rect.y - x.y));
				
				if(x.x <= this.rect.x)
					x = new Rectangle(this.rect.x, x.y, x.width - (this.rect.x - x.x), x.height);
				
				if(x.getMaxX() >= this.rect.getMaxX())
					x.width = x.width - (int)(x.getMaxX() - this.rect.getMaxX());
				

				if(x.getMaxY() >= this.rect.getMaxY())
					x.height = x.height - (int)(x.getMaxY() - this.rect.getMaxY());
				
				g2d.fillRect(x.x, x.y, x.width, x.height);
			}
			
				
		}
	}

	
	public Rectangle Redimencionar(Rectangle r)
	{
		return new Rectangle(
				(int)(r.x*proporcao),
				(int)(r.y*proporcao),
				(int)(r.width*proporcao),
				(int)(r.height*proporcao)
				);
	}
}
